﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Lidgren.Library.Network;
using System.Threading;
using BBCommon;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            int Port;

            try
            {
                Port = int.Parse(tbPort.Text);
            }
            catch
            {
                MessageBox.Show(this, "Please enter a number as the listenning port !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Program.ServerBB.StartServer(Port);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            // shutdown; sends disconnect to all connected clients with this reason string
            Program.ServerBB.Stop("Server stopped");
        }

        private void btnKick_Click(object sender, EventArgs e)
        {
            Program.ServerBB.KickUser((string)lbUserList.SelectedItem, "");
            btnRefreshInfo_Click(null, null);
        }

        private void lbUserList_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRefreshInfo_Click(sender, e);
        }

        private void btnRefreshInfo_Click(object sender, EventArgs e)
        {
            if (lbUserList.SelectedItem != null)
            {
                tbUserInfo.Text = Program.ServerBB.GetUserInfo((string)lbUserList.SelectedItem);
            }
            else
            {
                tbUserInfo.Text = "";
            }
        }

        private void btnSendMsgToUser_Click(object sender, EventArgs e)
        {
            if (lbUserList.SelectedItem != null)
            {
                if (tbMsg.Text != "")
                    Program.ServerBB.SendChat((string)lbUserList.SelectedItem, tbMsg.Text);

                tbMsg.Text = "";
            }
        }

        private void btnBroadcastMsg_Click(object sender, EventArgs e)
        {
            if( tbMsg.Text != "" )
                Program.ServerBB.SendChat(null, tbMsg.Text);

            tbMsg.Text = "";
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
            }
        }

        private void btnStats_Click(object sender, EventArgs e)
        {
            Program.ServerBB.SendStats();
        }
    }
}
